<?php
/**
 * Created by JetBrains PhpStorm.
 * User: q
 * Date: 13-12-30
 * Time: 下午2:01
 * 表area_count模型类
 */
class Area_model extends CI_Model{
    function __construct(){
        parent::__construct();
    }

    /**
     * 判断是否有该地区记录
     * @return count
     */
    function check_count(){
        $query = $this->db->get_where('area_count', array('area' => $_POST['quhao']));
        $result = $query->row_array();
        if(!empty($result)){
            return $result['count']+1;
        }else{
            return false;
        }
    }

    /**
     * 获取该地区学校数量
     * @return count|int
     */
    function get_count(){
        $check = $this->check_count();
        if($check){
            $this->plus_one();
            return $check;
        }else{
            $this->increase();
            return 1;
        }
    }

    /**
     * 该地区学校数量加1
     */
    function plus_one(){
        $this->db->where('area',$_POST['quhao']);
        $this->db->set('count', 'count+1', FALSE);
        $this->db->update('area_count');
    }

    /**
     * 添加一条新地区，学校数量为1
     */
    function increase(){
        $data = array(
            'area' => $_POST['quhao'] ,
            'count' => 1
        );
        $this->db->insert('area_count', $data);
    }
}